<template>
  <el-drawer
    :title="refForm.id?$t('message.edit' ): $t('message.add' )"
    :close-on-click-modal="modalDialog"
    :close-on-press-escape="modalDialog"
    :visible.sync="showApplication"
    size="600px"
    @close="closeDrawer"
  >
    <el-form ref="ruleForm" :model="refForm" :rules="rules" label-width="auto" label-suffix="：">
      <el-form-item :label="$t('message.qcmc')" prop="name">
        <el-input
          v-model="refForm.name"
          maxlength="50"
          style="width: 100%;"
          :placeholder="$t('message.qsr') + $t('message.qcmc')"
        />
      </el-form-item>
      <el-form-item :label="$t('message.gzmc')" prop="ruleId">
        <el-select v-model="refForm.ruleId" filterable :placeholder="$t('message.qxz') + $t('message.guizei')" style="width: 100%;">
          <el-option
            v-for="item in ruleList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('message.qckc')" prop="stock">
        <el-input
          v-model="refForm.stock"
          maxlength="50"
          style="width: 100%;"
          :placeholder="$t('message.qsr') + $t('message.qckc')"
          oninput="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
        />
      </el-form-item>
      <el-form-item v-if="refForm.id" :label="$t('message.sykc')" prop="surplusStock">
        <el-input
          v-model="refForm.surplusStock"
          maxlength="50"
          style="width: 100%;"
          :placeholder="$t('message.qsr') + $t('message.sykc')"
          oninput="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
        />
      </el-form-item>
      <el-form-item :label="$t('message.code')" prop="code">
        <el-input
          v-model="refForm.code"
          maxlength="50"
          style="width: 100%;"
          :placeholder="$t('message.qsr') + $t('message.code')"
        />
      </el-form-item>
      <el-form-item :label="$t('message.sort')" prop="sort">
        <el-input
          v-model="refForm.sort"
          maxlength="50"
          style="width: 100%;"
          :placeholder="$t('message.qsr') + $t('message.sort')"
          oninput="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
        />
      </el-form-item>
      <el-form-item :label="$t('message.status')">
        <el-switch v-model="refForm.status" :active-value="'1'" :inactive-value="'0'" />
      </el-form-item>
      <el-form-item>
        <el-button @click="closeDrawer">{{ $t('message.qx') }}</el-button>
        <el-button v-preventReClick :loading="subLoading" type="primary" @click="onSubmit">{{ $t('message.baocun') }}</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<script>
import {
  leaseRuleList, equipmentSaveOrUpdate
} from '@/api'
import store from '@/store'
export default {
  data() {
    return {
      showApplication: false,
      subLoading: false,
      modalDialog: store.getters.dialog,
      refForm: {},
      ruleList: [],
      rules: {
        name: [{ required: true, message: this.$t('message.qsr') + this.$t('message.qcmc'), trigger: 'blur' }],
        stock: [{ required: true, message: this.$t('message.qsr') + this.$t('message.qckc'), trigger: 'blur' }],
        surplusStock: [{ required: true, message: this.$t('message.qsr') + this.$t('message.sykc'), trigger: 'blur' }],
        ruleId: [{ required: true, message: this.$t('message.qxz') + this.$t('message.guizei'), trigger: 'change' }]
      }
    }
  },
  methods: {
    onSubmit() {
      const param = { ...this.refForm }
      const that = this
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (param.id) {
            that.subLoading = true
            equipmentSaveOrUpdate({ param: param })
              .then((res) => {
                // console.log(res)
                that.subLoading = false
                if (res.errorCode === '000000') {
                  that.$message.success(this.$t('message.editcg'))
                  setTimeout(() => {
                    that.$emit('close')
                  }, 300)
                }
              })
              .catch((er) => {
                that.subLoading = false
                console.log(er)
              })
          } else {
            that.subLoading = true
            equipmentSaveOrUpdate({
              param: param
            })
              .then((res) => {
                that.subLoading = false
                if (res.errorCode === '000000') {
                  that.$message.success(this.$t('message.addcg'))
                  setTimeout(() => {
                    that.$emit('close')
                  }, 300)
                }
              })
              .catch((err) => {
                that.subLoading = false
                console.log(err)
              })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    showClick(e) {
      this.showApplication = true
      this.getRuleList()
      this.refForm = e || { status: '1' }
      this.$nextTick(() => {
        this.$refs.ruleForm.clearValidate()
      })
    },
    // 列表
    getRuleList() {
      leaseRuleList({ param: { status: '1' }})
        .then((res) => {
          if (res.errorCode !== '000000') return
          this.ruleList = res.recordList || []
        })
    },
    closeDrawer() {
      this.$refs.ruleForm.clearValidate()
      this.showApplication = false
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/variables.scss";

</style>
